jQuery('document').ready(init);
function init(){
	bodyResize();
	newsInit();
	loginFrameInit();
	registerFrameInit();
	function drawUnderline(){
		var canvas = document.getElementById('underlineCanvas');
		if(!canvas.getContext){
			jQuery('#header').css('border-bottom','solid 2px red');
			return;
		}
		canvas.width = 1200;
		canvas.height = 2;
		var ctx = canvas.getContext('2d');
		var grd = ctx.createLinearGradient(0,0,1200,0);
		grd.addColorStop(0/8,'#0000ff'),
		grd.addColorStop(1/8,'#70db93'),
		grd.addColorStop(2/8,'#00ffff'),
		grd.addColorStop(3/8,'#ff0000'),
		grd.addColorStop(4/8,'#a67d3d'),
		grd.addColorStop(5/8,'#ffff00'),
		grd.addColorStop(6/8,'#d9d919'),
		grd.addColorStop(7/8,'#00ff00'),
		grd.addColorStop(8/8,'#8c7853'),
		ctx.fillStyle = grd;
		ctx.fillRect(0,0,1200,2);
	}
	drawUnderline();
}
function newsInit(){
	var currentNewsNum = -1;
	var totalNews = 3;
	var autoChange;
	function showNews(num){
		if(num == currentNewsNum){return;}
		if(currentNewsNum >= 0){
			jQuery('#news'+currentNewsNum).fadeOut(1000);
			jQuery('#item'+currentNewsNum).css('opacity','0.4');
		}
		jQuery('#news'+num).fadeIn(1000);
		jQuery('#item'+num).css('opacity','1');
		jQuery('#item'+num).css('margin-left','125px');
		var leftNum = (num+totalNews-1)%totalNews;
		jQuery('#item'+leftNum).css('margin-left','18px');
		var rightNum = (num+1)%totalNews;
		jQuery('#item'+rightNum).css('margin-left','232px');
		currentNewsNum = num;
		autoChange = setTimeout(autoChangeNews,5000);
	}
	function autoChangeNews(){
		var next = (currentNewsNum + 1)%totalNews;
		showNews(next);
	}
	for(var i = 0; i < totalNews; i++){
		jQuery('#'+'item'+i).bind('click',function(){
			var id = this.id;
			var num = parseInt(id.substring(4,id.length));
			clearTimeout(autoChange);
			showNews(num);
		})
	}
	jQuery('#lastItem').bind('click',function(){
		var last = (currentNewsNum+totalNews-1)%totalNews;
		clearTimeout(autoChange);
		showNews(last);
	});
	jQuery('#nextItem').bind('click',function(){
		var next = (currentNewsNum+1)%totalNews;
		clearTimeout(autoChange);
		showNews(next);
	});
	showNews(1);
}
function loginFrameInit(){
	function drawLoginFrameExitButton(){
		var canvas = document.getElementById('loginFrameExitCanvas');
		canvas.height = 21;
		canvas.width = 21;
		var ctx = canvas.getContext('2d');
		ctx.strokeStyle = '#333333';
		ctx.lineWidth = 5;
		ctx.lineCap = 'round';
		ctx.moveTo(3,3);
		ctx.lineTo(18,18);
		ctx.moveTo(18,3);
		ctx.lineTo(3,18);
		ctx.stroke();
	}
	function showLoginFrame(){
		if(jQuery('#loginFrame').css('display') == 'block'){
			return;
		}
		jQuery('#registerFrameExitButton').click();
		jQuery('#loginFrame').css('display','block');
		var top = -320;
		var goal = 50;
		var speed = 30;
		appear();
		function appear(){
			if(goal-top>=speed){
				top += speed;
				jQuery('#loginFrame').css('margin-top',top+'px');
				setTimeout(appear,25);
			}
			else{
				top = goal;
				jQuery('#loginFrame').css('margin-top',top+'px');
			}
		}
	}
	function doLogin(){
		var eMode = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var pMode = /^[0-9a-zA-Z]{6,16}$/;
		var email = jQuery('#loginEmailInput').val();
		if(!eMode.test(email)){
			jQuery('#loginErrorMessage').html('邮箱或密码不正确');
			return;
		}
		var password = jQuery('#loginPasswordInput').val();
		if(!pMode.test(password)){
			jQuery('#loginErrorMessage').html('邮箱或密码不正确');
			return;
		}
		jQuery('#loginForm').ajaxSubmit(function(data){
			switch(data){
				case 'SUCCESS':
					window.location.reload();
					break;
				case 'LOGIN_WRONG_EMAIL_PASSWD':
					needToChange = true;
					jQuery('#loginErrorMessage').html('邮箱或密码不正确');
					break;
				default:
					break;
			}
		});
	}
	drawLoginFrameExitButton();
	jQuery('#loginFrame').draggable();
	jQuery('#loginButton').bind('click',function(){
		showLoginFrame();
		jQuery('#loginEmailInput').focus();
	});
	jQuery('#loginFrameLoginButton').bind('click',function(){
		doLogin();
	});
	jQuery('#loginFrameExitButton').bind('click',function(){
		jQuery('#loginFrame').css('display','none');
		jQuery('#loginFrame').css('top','0px');
		jQuery('#loginFrame').css('left','0px');
	});
	jQuery('#loginFrameResetButton').bind('click',function(){
		jQuery('#loginEmailInput').val('');
		jQuery('#loginPasswordInput').val('');
		jQuery('#loginErrorMessage').html('');
		canLogin = [0,0];
		jQuery('#loginEmailInput').focus();
	});
}
function registerFrameInit(){
	var aMode = /^[a-zA-Z]{1}[a-zA-Z0-9_\.\-]{1,17}$/;
	var pMode = /^[0-9a-zA-Z]{6,16}$/;
	var eMode = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var kMode = /^[0-9a-zA-Z]{4}$/;
	var errorInfo = ['用户名需以字符开头，2~18位的数字或字符组成',
	                 '密码需由长度6~16的字符或数字组成',
	                 '请再次输入相同密码',
	                 '请输入您的正确邮箱',
	                 '请输入您的学校',
	                 '请输入右图中的4位验证码'];
	function drawRegisterFrameExitButton(){
		var canvas = document.getElementById('registerFrameExitCanvas');
		canvas.height = 21;
		canvas.width = 21;
		var ctx = canvas.getContext('2d');
		ctx.strokeStyle = '#333333';
		ctx.lineWidth = 5;
		ctx.lineCap = 'round';
		ctx.moveTo(3,3);
		ctx.lineTo(18,18);
		ctx.moveTo(18,3);
		ctx.lineTo(3,18);
		ctx.stroke();
	}
	function showRegisterFrame(){
		if(jQuery('#registerFrame').css('display') == 'block'){
			return;
		}
		jQuery('#loginFrameExitButton').click();
		jQuery('#registerFrame').css('display','block');
		var top = -320;
		var goal = 50;
		var speed = 30;
		appear();
		function appear(){
			if(goal-top>=speed){
				top += speed;
				jQuery('#registerFrame').css('margin-top',top+'px');
				setTimeout(appear,25);
			}
			else{
				top = goal;
				jQuery('#registerFrame').css('margin-top',top+'px');
			}
		}
	}
	function doRegister(){
		var target = ['registerAccountInput',
		              'registerPasswordInput',
		              'registerPasswordRepeatInput',
		              'registerEmailInput',
		              'registerSchoolInput',
		              'registerKaptchaInput'];
		var account = jQuery('#registerAccountInput').val();
		if(!aMode.test(account)){
			jQuery('#registerAccountInput').focus();
			return;
		}
		var password = jQuery('#registerPasswordInput').val();
		if(!pMode.test(password)){
			jQuery('#registerPasswordInput').focus();
			return;
		}
		var passwordRepeat = jQuery('#registerPasswordRepeatInput').val();
		if(passwordRepeat != password){
			jQuery('#registerPasswordRepeatInput').focus();
			return;
		}
		var email = jQuery('#registerEmailInput').val();
		if(!eMode.test(email)){
			jQuery('#registerEmailInput').focus();
			return;
		}
		var school = jQuery('#registerSchoolInput').val();
		if(school == ''){
			jQuery('#registerSchoolInput').focus();
			return;
		}
		var kaptcha = jQuery('#registerKaptchaInput').val();
		if(!kMode.test(kaptcha)){
			jQuery('#registerKaptchaInput').focus();
			return;
		}
		jQuery('#registerForm').ajaxSubmit(function(data){
			switch(data)
			{
			case "SUCCESS":
				jQuery('#loginEmailInput').val(jQuery('#registerEmailInput').val());
				jQuery('#loginErrorMessage').html('注册成功请登录!');
				jQuery('#loginButton').click();
				jQuery('#loginPasswordInput').focus();
				break;
			case "FORM_FIELD_FORMAT_INCORRECT":
				jQuery('#registerErrorMessage').html('表单格式不对');
				break;
			case "FORM_KAPTCHA_INCORRECT":
				jQuery('#registerErrorMessage').html('验证码不正确');
				break;
			case "REGISTER_EMAIL_REPEAT":
				jQuery('#registerErrorMessage').html('邮箱已被注册');
				break;
			default:
				break;
			}
		});
	}
	function changeCode(){
	    $('#kaptchaImage').attr('src', 'kaptcha/getKaptcha.do?' + Math.floor(Math.random()*100) );  
	    event.cancelBubble=true;  
	}
	drawRegisterFrameExitButton();
	jQuery('#registerFrame').draggable();
	jQuery('#registerBirthdayInput').datepicker({
		yearRange: "-100:+0",
		showOtherMonths: true,
	    selectOtherMonths: true,
		dateFormat: "yy-mm-dd",
		changeMonth:true,
		changeYear:true,
		monthNamesShort: [ "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" ],
		dayNamesMin: [ "日", "一", "二", "三", "四", "五", "六" ]
	});
	jQuery('#registerButton').bind('click',function(){
		showRegisterFrame();
		jQuery('#registerAccountInput').focus();
	});
	jQuery('#registerFrameRegButton').bind('click',function(){
		doRegister();
	});
	jQuery('#registerFrameExitButton').bind('click',function(){
		jQuery('#registerFrame').css('display','none');
		jQuery('#registerFrame').css('top','0px');
		jQuery('#registerFrame').css('left','0px');
	});
	jQuery('#registerFrameResetButton').bind('click',function(){
		jQuery('#registerAccountInput').val('');
		jQuery('#registerEmailInput').val('');
		jQuery('#registerPasswordInput').val('');
		jQuery('#registerPasswordRepeatInput').val('');
		jQuery('#registerSchoolInput').val('');
		jQuery('#registerKaptchaInput').val('');
		jQuery('#registerAccountInfoImage').css('background-image','');
		jQuery('#registerPasswordInfoImage').css('background-image','');
		jQuery('#registerPasswordRepeatInfoImage').css('background-image','');
		jQuery('#registerEmailInfoImage').css('background-image','');
		jQuery('#registerSchoolInfoImage').css('background-image','');
		jQuery('#registerAccountInput').focus();
	});
	jQuery('#registerAccountInput').bind('input',function(){
		var account = jQuery('#registerAccountInput').val();
		if(aMode.test(account)){
			jQuery('#registerAccountInfoImage').css('background-image','url(resources/images/right.png)');
		}
		else{
			jQuery('#registerAccountInfoImage').css('background-image','url(resources/images/error.png)');
		}
	});
	jQuery('#registerAccountInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[0]);
	});
	jQuery('#registerPasswordInput').bind('input',function(){
		var password = jQuery('#registerPasswordInput').val();
		if(pMode.test(password)){
			jQuery('#registerPasswordInfoImage').css('background-image','url(resources/images/right.png)');
		}
		else{
			jQuery('#registerPasswordInfoImage').css('background-image','url(resources/images/error.png)');
		}
	});
	jQuery('#registerPasswordInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[1]);
	});
	jQuery('#registerPasswordRepeatInput').bind('input',function(){
		var password = jQuery('#registerPasswordInput').val();
		var passwordRepeat = jQuery('#registerPasswordRepeatInput').val();
		if(password == passwordRepeat){
			jQuery('#registerPasswordRepeatInfoImage').css('background-image','url(resources/images/right.png)');
		}
		else{
			jQuery('#registerPasswordRepeatInfoImage').css('background-image','url(resources/images/error.png)');
		}
	});
	jQuery('#registerPasswordRepeatInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[2]);
	});
	jQuery('#registerEmailInput').bind('input',function(){
		var email = jQuery('#registerEmailInput').val();
		if(eMode.test(email)){
			jQuery('#registerEmailInfoImage').css('background-image','url(resources/images/right.png)');
		}
		else{
			jQuery('#registerEmailInfoImage').css('background-image','url(resources/images/error.png)');
		}
	});
	jQuery('#registerEmailInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[3]);
	});
	jQuery('#registerSchoolInput').bind('input',function(){
		var school = jQuery('#registerSchoolInput').val();
		if(school != ''){
			jQuery('#registerSchoolInfoImage').css('background-image','url(resources/images/right.png)');
		}
		else{
			jQuery('#registerSchoolInfoImage').css('background-image','url(resources/images/error.png)');
		}
	});
	jQuery('#registerSchoolInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[4]);
	});
	jQuery('#registerKaptchaInput').bind('focus',function(){
		jQuery('#registerErrorMessage').html(errorInfo[5]);
	});
	jQuery('#changeCode').bind('click',function(){
		changeCode();
		jQuery('#registerKaptchaInput').focus();
	});
	jQuery('#kaptchaImage').bind('click',function(){
		changeCode();
		jQuery('#registerKaptchaInput').focus();
	});
}
function bodyResize(){
	pageWidth = window.innerWidth;
	if(typeof pageWidth != "number"){ 
		if(document.compatMode == "number"){
			pageWidth = document.documentElement.clientWidth; 
		}else{ 
			pageWidth = document.body.clientWidth; 
		} 
	}
	var Left = Math.max((pageWidth-1200)/2,0);
	jQuery('#mainContent').css('margin-left',Left+'px');
	jQuery('#loginFrame').css('margin-left',(pageWidth-400)/2+'px');
	jQuery('#registerFrame').css('margin-left',(pageWidth-400)/2+'px');
}